package com.bdqn.t382.lx.service.impl;

import com.bdqn.t382.lx.mapper.SupplierMapper;
import com.bdqn.t382.lx.pojo.Supplier;
import com.bdqn.t382.lx.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;

    public void setSupplierMapper(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }

    @Override
    public List<Supplier> findAll(Supplier supplier) {
        return supplierMapper.selectAll(supplier);
    }

    @Transactional
    @Override
    public int insert(List<Supplier> suppliers) {
        int rows = 0;
        for (int i = 0; i < suppliers.size(); i++) {
            Supplier supplier = suppliers.get(i);
            if (i == 1) {
                throw new RuntimeException("数据库异常");
            }
            rows += supplierMapper.insert(supplier);
        }
        return rows;
    }

    @Transactional
    @Override
    public int delete(int id) {
        return supplierMapper.delete(id);
    }

    @Transactional
    @Override
    public int update(Supplier supplier) {
        return supplierMapper.update(supplier);
    }

}
